package com.platform.qujia.service.impl;
import com.platform.qujia.config.BasicInfo;
import com.platform.qujia.constantEnum.PayWayPayTypeEnum;
import com.platform.qujia.dao.*;
import com.platform.qujia.param.request.AddPayOrderParam;
import com.platform.qujia.pojo.*;
import com.platform.qujia.service.EnterpriseService;
import com.platform.qujia.utils.redis.RedisCacheUtil;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service("enterpriseService")
public class EnterpriseServiceImpl implements EnterpriseService {

    @Resource
    private EnterpriseMapper enterpriseMapper;

    @Resource
    private StationMapper stationMapper;

    @Resource
    private StaOilGunMapper staOilGunMapper;

    @Resource
    private RedisCacheUtil redisCacheUtil;

    private static Logger logger =(Logger) LoggerFactory.getLogger(EnterpriseServiceImpl.class);

    @Override
    public List<ShiftClass> selectShiftClassList(ShiftClass shiftClass) {
        return enterpriseMapper.selectShiftClassList(shiftClass);
    }

    @Override
    public List<Oil> selectOilInfoList() {
        return stationMapper.selectOilInfoList();
    }

//    //获取油企信息
//    @Override
//    @Cacheable(value = { "EnterpriseServiceImpl_selectEnterpriseInfo"},
//            key="'e_' + #root.args[0]",
//            cacheManager = "cacheManager1H",
//            condition = "#root.args[0] != null",
//            unless = "#result == null"
//    )
    public Enterprise selectEnterpriseInfo(Integer enterpriseId) {
        Enterprise tmp = enterpriseMapper.selectEnterpriseInfo(enterpriseId);
//        if("1".equals(tmp.getIsPlatformFlag())){
//            // 平台模式特殊处理
//            tmp.setAppId("wx1ff877efba5ddec8");
//            tmp.setAppSecret("376a0bf764de20c0af5e8466266f1ee4");
//            tmp.setOriginId("gh_5cb9e8b4441d");
//            tmp.setGzhAppId("wx2c022df3e1bc7263");
//            tmp.setGzhAppSecret("61342bdfcdf82bed50dff0eb5094127f");
//            tmp.setCardId("pB0HZ6e__exXYhf-Lxp3FLEtwpr4");
////            tmp.setTemplateId("");
//        };
        return tmp;
    }

    @Override
    public Enterprise selectEnterpriseByCardId(String cardId) {
        return enterpriseMapper.selectEnterpriseByCardId(cardId);
    }

    @Override
    public StaOilGun selectStaGunInfo(StaOilGun staOilGun) {
        return staOilGunMapper.selectStaGunInfo(staOilGun);
    }



    /**
     * 查询油站信息
     * @param stationId 油站信息ID
     * @return 油企信息
     */
    @Override
//    @Cacheable(value = { "EnterpriseServiceImpl_selectStationById"},
//            key="'s_' + #root.args[0]",
//            cacheManager = "cacheManager1H",
//            condition = "#root.args[0] != null",
//            unless = "#result == null"
//    )
    public Station selectStationById(Integer stationId) {
        return stationMapper.selectStationById(stationId);
    }

    //获取油站列表
    @Override
//    @Cacheable(value = { "EnterpriseServiceImpl_selectStaInfoList"},
//            key="'e_' + #root.args[0].getEnterpriseId()",
//            cacheManager = "cacheManager1H",
//            condition = "#root.args[0].getEnterpriseId() != null",
//            unless = "#result == null"
//    )
    public List<Station> selectStaInfoList(Station station) {
        return stationMapper.selectStaInfoList(station);
    }

    //获取油站油枪列表信息
    @Override
    @Cacheable(value = { "EnterpriseServiceImpl_selectStaOilGunList"},
            key="'s_' + #root.args[0].getStationId()",
            cacheManager = "cacheManager1H",
            condition = "#root.args[0].getGunList() == null && #root.args[0].getStationId() != null",
            unless = "#result == null"
    )
    public List<StaOilGun> selectStaOilGunList(StaOilGun  staOilGun) {
        return staOilGunMapper.selectStaOilGunList(staOilGun);
    }

    public List<StaOilGun> selectStaOilGunListCache(String stationId, String userId, StaOilGun staOilGun){
        String gunNo="";
        if(staOilGun.getGunList()!=null&&staOilGun.getGunList().length>0){
            gunNo = StringUtils.join(staOilGun.getGunList(), ",");
        }

        String key = "price:"+ stationId + ":" + userId + ":"+gunNo;

        if(redisCacheUtil.hasKey(key)){
            return redisCacheUtil.getCacheList(key);
        }
        List<StaOilGun> staOilGunList = staOilGunMapper.selectStaOilGunList(staOilGun);
        redisCacheUtil.setCacheList(key, staOilGunList);
        redisCacheUtil.expire(key,10, TimeUnit.HOURS);
        return staOilGunList;
    };


    @Override
//    @Cacheable(value = { "EnterpriseServiceImpl_selectStaPayWay"},
//            key="'s_' + #root.args[0].getStationId() + 'a_' + #root.args[0].getAppType() + 'u_' + #root.args[0].getUseType",
//            cacheManager = "cacheManager1H",
//            condition = " #root.args[0].getStationId() != null && #root.args[0].getAppType() != null && #root.args[0].getUseType() != null",
//            unless = "#result == null"
//    )
    public List<PayWay> selectStaPayWay(PayWay payWay) {
        List <PayWay> tmp = stationMapper.selectStaPayWay(payWay);
//        for (int i = 0; i < tmp.size(); i++) {
//            Enterprise ent = tmp.get(i).getEnterprise();
//            if("1".equals(ent.getIsPlatformFlag())){
//                ent.setAppId("wx1ff877efba5ddec8");
//                ent.setAppSecret("376a0bf764de20c0af5e8466266f1ee4");
//                ent.setOriginId("gh_5cb9e8b4441d");
//                ent.setGzhAppId("wx2c022df3e1bc7263");
//                ent.setGzhAppSecret("61342bdfcdf82bed50dff0eb5094127f");
//                ent.setCardId("pB0HZ6e__exXYhf-Lxp3FLEtwpr4");
//            }
//        }
        return tmp;
    }


    @Override
    @Cacheable(value = { "EnterpriseServiceImpl_selectOilByOilId"},
            key="'oilid_' + #root.args[0]",
            cacheManager = "cacheManager1D",
            condition = " #root.args[0] != null",
            unless = "#result == null"
    )
    public Oil selectOilByOilId(Integer oilId) {
        return staOilGunMapper.selectOil(Oil.builder().oilId(oilId).build()).get(0);
    }

    @Override
    @Cacheable(value = { "EnterpriseServiceImpl_selectOilByOilName"},
            key="'oilname_' + #root.args[0]",
            cacheManager = "cacheManager1D",
            condition = " #root.args[0] != null",
            unless = "#result == null"
    )
    public Oil selectOilByOilName(String oilName) {
        return staOilGunMapper.selectOil(Oil.builder().oilName(oilName).build()).get(0);
    }



    //获取油企信息
    @Override
    @Cacheable(value = { "EnterpriseServiceImpl_selectEnterpriseByOriginId"},
            key="'e_' + #root.args[0]",
            cacheManager = "cacheManager15M",
            condition = "#root.args[0] != null",
            unless = "#result == null"
    )
    public Enterprise selectEnterpriseByOriginId(String originId) {
        Enterprise tmp = enterpriseMapper.selectEnterpriseByOriginId(originId);
//        if("1".equals(tmp.getIsPlatformFlag())){
//            // 平台模式特殊处理
//            tmp.setAppId("wx1ff877efba5ddec8");
//            tmp.setAppSecret("376a0bf764de20c0af5e8466266f1ee4");
//            tmp.setOriginId("gh_5cb9e8b4441d");
//            tmp.setGzhAppId("wx2c022df3e1bc7263");
//            tmp.setGzhAppSecret("61342bdfcdf82bed50dff0eb5094127f");
//            tmp.setCardId("pB0HZ6e__exXYhf-Lxp3FLEtwpr4");
////            tmp.setTemplateId("");
//        };
        return tmp;
    }

    @Override
    public CustomerDialogueAdTemplate selectCustomerDialogueAdTemplateById(Long adId) {
        return enterpriseMapper.selectCustomerDialogueAdTemplateById(adId);
    }

	@Override
	public PayWay selectOneStaPayWay(PayWay payWay) {
		return stationMapper.selectOneStaPayWay(payWay);
	}

	@Override
	public void isSetMno(AddPayOrderParam payParameter) {
		SelectPayWayEnterpriseVO vo=new SelectPayWayEnterpriseVO();
		PayWay payWay=new PayWay();
		payWay.setUseType("1");             //使用类型
		payWay.setStationId(payParameter.getStationId());
		payWay.setPayType(payParameter.getPayTypeEnum().getValue().toString());
		PayWay payWayList = stationMapper.selectOneStaPayWay(payWay); //获取该油站的支付类型
		if(payWayList==null) {
			logger.info("获取不到PayWay对象");
			return ;
		}
		payParameter.setMno(payWayList.getMno());
	}

}
